package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceMaterialCompleteCheckRpcService;
import com.hunttown.mes.rpc.domain.MesProduceMaterialCompleteCheckDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceMaterialCompleteCheckDTOQuery;
import com.hunttown.mes.service.MesProduceMaterialCompleteCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProduceMaterialCompleteCheckConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:28:27
 */
@Service
public class MesProduceMaterialCompleteCheckRpcServiceImpl implements MesProduceMaterialCompleteCheckRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceMaterialCompleteCheckRpcServiceImpl.class);

    private final MesProduceMaterialCompleteCheckService mesProduceMaterialCompleteCheckService;

    @Autowired
    public MesProduceMaterialCompleteCheckRpcServiceImpl(MesProduceMaterialCompleteCheckService mesProduceMaterialCompleteCheckService) {
        this.mesProduceMaterialCompleteCheckService = mesProduceMaterialCompleteCheckService;
    }

    @Override
    public MesProduceMaterialCompleteCheckDTO insert(MesProduceMaterialCompleteCheckDTO mesProduceMaterialCompleteCheckDTO) {
        try {
            return convert(mesProduceMaterialCompleteCheckService.insert(convert(mesProduceMaterialCompleteCheckDTO)));
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck create failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceMaterialCompleteCheckDTO mesProduceMaterialCompleteCheckDTO) {
        try {
            return mesProduceMaterialCompleteCheckService.updateInfoById(convert(mesProduceMaterialCompleteCheckDTO));
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck update by id failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceMaterialCompleteCheckDTOQuery mesProduceMaterialCompleteCheckDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceMaterialCompleteCheckDTOQuery.getW_id() != null && mesProduceMaterialCompleteCheckDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceMaterialCompleteCheckDTOQuery.getW_id());
            }

            if (mesProduceMaterialCompleteCheckDTOQuery.getIds() != null && !mesProduceMaterialCompleteCheckDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceMaterialCompleteCheckDTOQuery.getIds());
            }

            if (mesProduceMaterialCompleteCheckDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceMaterialCompleteCheckDTOQuery.getStopFlag());
            }

            if (mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag());
            }

            return mesProduceMaterialCompleteCheckService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck update by query failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceMaterialCompleteCheckDTO selectById(Integer id) {
        try {
            return convert(mesProduceMaterialCompleteCheckService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck select by id failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceMaterialCompleteCheckDTO selectObjByQuery(MesProduceMaterialCompleteCheckDTOQuery mesProduceMaterialCompleteCheckDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceMaterialCompleteCheckDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceMaterialCompleteCheckDTOQuery.getStopFlag());
            }

            if (mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceMaterialCompleteCheckDTOQuery.getOrderBy());
            }

            return convert(mesProduceMaterialCompleteCheckService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck select obj by query failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceMaterialCompleteCheckDTO> getListForPage(MesProduceMaterialCompleteCheckDTOQuery mesProduceMaterialCompleteCheckDTOQuery) {
        Query query = Query.page(mesProduceMaterialCompleteCheckDTOQuery.getPn(), mesProduceMaterialCompleteCheckDTOQuery.getPageSize());

        if (mesProduceMaterialCompleteCheckDTOQuery.getId() != null && mesProduceMaterialCompleteCheckDTOQuery.getId() > 0) {
            query.put("id", mesProduceMaterialCompleteCheckDTOQuery.getId());
        }

        if (mesProduceMaterialCompleteCheckDTOQuery.getIds() != null && !mesProduceMaterialCompleteCheckDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceMaterialCompleteCheckDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceMaterialCompleteCheckDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceMaterialCompleteCheckDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceMaterialCompleteCheckDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceMaterialCompleteCheckDTOQuery.getEndDate());
        }

        if (mesProduceMaterialCompleteCheckDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceMaterialCompleteCheckDTOQuery.getStopFlag());
        }

        if (mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceMaterialCompleteCheckDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceMaterialCompleteCheckDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceMaterialCompleteCheckDTOQuery.getOrderBy());
        }

        List<MesProduceMaterialCompleteCheckDTO> list = convert(mesProduceMaterialCompleteCheckService.selectByQuery(query));
        Page<MesProduceMaterialCompleteCheckDTO> page = Page.from(mesProduceMaterialCompleteCheckDTOQuery.getPn(), mesProduceMaterialCompleteCheckDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceMaterialCompleteCheckService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceMaterialCompleteCheckService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceMaterialCompleteCheck delete failed.", e);
            throw new RuntimeException("MesProduceMaterialCompleteCheck delete failed, Please order your code.", e);
        }
    }
}